<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>批次管理</title>
    <jsp:include page="/resources/inc/head.jsp" flush="true"/>
</head>
<body>
<div id="main">
    <div id="toolbar">
        <shiro:hasPermission name="umbrella:batch:create"><a class="waves-effect waves-button" href="javascript:;" onclick="createAction()"><i class="zmdi zmdi-plus"></i> 新增</a></shiro:hasPermission>
        <shiro:hasPermission name="umbrella:batch:update"><a class="waves-effect waves-button" href="javascript:;" onclick="updateAction()"><i class="zmdi zmdi-edit"></i> 编辑</a></shiro:hasPermission>
        <shiro:hasPermission name="umbrella:batch:delete"><a class="waves-effect waves-button" href="javascript:;" onclick="deleteAction()"><i class="zmdi zmdi-close"></i> 删除</a></shiro:hasPermission>
    </div>
    <table id="table"></table>
</div>
<jsp:include page="/resources/inc/footer.jsp" flush="true"/>
<jsp:include page="/resources/inc/export.jsp" flush="true"/>

<script>
    var $table = $('#table');
    $(function() {
        // bootstrap table初始化
        $table.bootstrapTable({
            url: '${basePath}/manage/batch/list',
            height: getHeight(),
            detailFormatter: 'detailFormatter',
            paginationLoop: false,
            silentSort: false,
            smartDisplay: false,
            escape: true,
            searchOnEnterKey: true,
            maintainSelected: true,
            toolbar: '#toolbar',               //工具按钮用哪个容器
            striped: true,                      //是否显示行间隔色
            cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            sortable: true,                     //是否启用排序
            sortOrder: "asc",                   //排序方式
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,                      //初始化加载第一页，默认第一页,并记录
            pageSize: 10,                     //每页的记录行数（*）
            pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
            search: false,                      //是否显示表格搜索
            strictSearch: true,
            showColumns: true,                  //是否显示所有的列（选择显示的列）
            showRefresh: true,                  //是否显示刷新按钮
            minimumCountColumns: 2,             //最少允许的列数
            clickToSelect: true,                //是否启用点击选中行
            showToggle: true,                   //是否显示详细视图和列表视图的切换按钮
            cardView: false,                    //是否显示详细视图
            detailView: false,                  //是否显示父子表
            idField: 'id',
            columns: [
                {field: 'ck', checkbox: true},
                {field: 'code', title: '批次号'},
                {field: 'costPrice', title: '成本价(元)'},
                {field: 'marketPrice', title: '市场价(元)'},
                {field: 'brand', title: '品牌'},
                {field: 'material', title: '材质'},
                {field: 'weights', title: '净重(kg)'},
                {field: 'size', title: '尺寸(cm)'},
                {field: 'color', title: '颜色'},
                {field: 'status', title: '状态',formatter: function (value, row, index) {
                    return getDictText("UPMS_STATUS",value);
                }},
                {field: 'description', title: '描述'},
                {field: 'action', title: '操作', align: 'center', formatter: 'actionFormatter', events: 'actionEvents', clickToSelect: true}
            ]
            <shiro:hasPermission name="umbrella:batch:export">
            ,
            //导出excel表格设置
            showExport: phoneOrPc(),              //是否显示导出按钮(此方法是自己写的目的是判断终端是电脑还是手机,电脑则返回true,手机返回falsee,手机不显示按钮)
            exportDataType: "basic",              //basic', 'all', 'selected'.
            exportTypes:['excel','xlsx'],	    //导出类型
            exportOptions:{
                //ignoreColumn: [0,0],            //忽略某一列的索引
                fileName: '批次数据导出',              //文件名称设置
                excelstyles: ['background-color', 'color', 'font-size', 'font-weight'],
                //onMsoNumberFormat: DoOnMsoNumberFormat
            }
            //导出excel表格设置
            </shiro:hasPermission>
        });
    });
    window.actionEvents = {
        <shiro:hasPermission name="umbrella:batch:update">
        'click .update': function(e, value, row, index) {
            updateAction([row]);
        },
        </shiro:hasPermission>
        <shiro:hasPermission name="umbrella:batch:delete">
        'click .delete': function(e, value, row, index) {
            deleteAction([row]);
        }
        </shiro:hasPermission>
    };

    // 格式化操作按钮
    function actionFormatter(value, row, index) {
        return [
            <shiro:hasPermission name="umbrella:batch:update">
            '<a class="update" href="javascript:;" data-toggle="tooltip" title="Edit"><i class="glyphicon glyphicon-edit"></i></a>　',
            </shiro:hasPermission>
            <shiro:hasPermission name="umbrella:batch:delete">
            '<a class="delete" href="javascript:;" data-toggle="tooltip" title="Remove"><i class="glyphicon glyphicon-remove"></i></a>'
            </shiro:hasPermission>
        ].join('');
    }
    // 新增
    var createDialog;
    function createAction() {
        createDialog = openDialog('url:${basePath}/manage/batch/create','新增')
    }
    // 编辑
    var updateDialog;
    function updateAction(rows) {
        if(!rows)
            rows = $table.bootstrapTable('getSelections');
        if (rows.length != 1) {
            infoDialog('请选择一条记录！');
        } else {
            updateDialog = openDialog('url:${basePath}/manage/batch/update/'+rows[0].id,'编辑');
        }
    }
    // 删除
    var deleteDialog;
    function deleteAction(rows) {
        if(!rows)
            rows = $table.bootstrapTable('getSelections');
        if (rows.length == 0) {
            infoDialog( '请至少选择一条记录！');
        } else {
            deleteDialog = confirmDialog('确认删除该批次吗？',function(){
                var ids = new Array();
                for (var i in rows) {
                    ids.push(rows[i].id);
                }
                $.ajax({dataType: 'json',
                    type: 'get',
                    url: '${basePath}/manage/batch/delete/' + ids.join("-"),
                    success: function(result) {
                        if (result.code != 1) {
                            if (result.data instanceof Array) {
                                $.each(result.data, function(index, value) {
                                    infoDialog(value.errorMsg);
                                });
                            } else {
                                infoDialog(result.data);
                            }
                        } else {
                            deleteDialog.close();
                            $table.bootstrapTable('refresh');
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        infoDialog(textStatus);
                    }
                });
            });
        }
    }
</script>
</body>
</html>